<template>
  <div class="mt10">
    <app-block>
      <zm-form :model="searchForm" ref="searchForm" inline>
        <zm-form-item prop="name">
          <zm-input
            v-model="searchForm.name"
            :placeholder="isDraft?'方案名称查询':'方案名称/ID查询'"
            class="w200"
          ></zm-input>
        </zm-form-item>
        <zm-form-item prop="exchangeRange" v-if="!isDraft">
          <zm-data-select
            v-model="searchForm.exchangeRange"
            :list="PRODUCT_EXCHANGE"
            class="w150"
            clearable
            placeholder="兑换范围"
            multiple
            str
          >
          </zm-data-select>
        </zm-form-item>
        <zm-form-item prop="type" v-if="!isDraft">
          <zm-data-select
            v-model="searchForm.type"
            :list="typeList"
            class="w150"
            clearable
            placeholder="产品类型"
          >
          </zm-data-select>
        </zm-form-item>
        <zm-form-item>
          <zm-button @click="onSearch" t="search"></zm-button>
          <zm-button @click="onReset" t="reset"></zm-button>
        </zm-form-item>
        <zm-form-item label="审批状态：" class="db" id="productConfigRadioNone">
          <zm-data-radio
            is-btn
            :list="CHECK_TYPE"
            v-model="searchForm.checkStatus"
            @change="onSearch"
          />
        </zm-form-item>
      </zm-form>
      <zm-table-data
        :data="tableParams.data"
        :columns="columns"
        :fetch="loadList"
        :table-params="tableParams"
        :show-page="true"
      >
      </zm-table-data>
    </app-block>

    <!-- 编辑特殊节假日 -->
    <set-special-rule
      v-model="specialRuleParams.visible"
      v-if="specialRuleParams.visible"
      :id="specialRuleParams.id"
      :params="specialRuleParams.params"
      @re-load="loadList"
    ></set-special-rule>
    <!-- 详情 -->
    <detail
    type="specialPrice"
      :is-sold-product="true"
      v-model="isShowDetail"
      v-if="isShowDetail"
      :id="detailID"
      :draft-id="draftID"
      :is-show-btn="false"
    ></detail>
  </div>
</template>

<script>
import tableMixin from '@/mixins/zmTableMixin'
import { typeList, columns } from './const'
import { CHECK_TYPE, PRODUCT_EXCHANGE, PROCESS_TYPE } from '@/model'
import productProgramApis from '@/api/src/productProgram'
import SetSpecialRule from '../SetSpecialRule'
import Detail from '@/components/DetailContainer'
import approveApis from '@/api/src/approve'
import DraftApis from '@/api/src/draft'
export default {
  name: 'PackageProduct',
  components: { SetSpecialRule, Detail },
  mixins: [tableMixin],
  data () {
    return {
      CHECK_TYPE,
      typeList,
      PRODUCT_EXCHANGE,
      PROCESS_TYPE,
      searchForm: {
        name: '',
        exchangeRange: '',
        type: '',
        checkStatus: '3'
      },
      detailID: '',
      draftID: '',
      isShowDetail: false,
      specialRuleParams: {
        visible: false
      }
    }
  },
  computed: {
    columns () {
      return columns(this)
    },
    isDraft () {
      return ['2', '1', '4'].includes(this.searchForm.checkStatus)
    }
  },
  watch: {},
  created () {},
  methods: {
    async loadList () {
      let _res
      if (this.isDraft) {
        _res = await DraftApis.getPageList(this.assignQuery({...this.searchForm, type: '2'}))
      } else {
        _res = await productProgramApis.getPageListByProduct(this.assignQuery(this.searchForm))
      }
      this.initialTableData(_res.list, _res.count)
    },
    onEdit (row) {
      this.specialRuleParams = {
        visible: true,
        id: row.id,
        params: {
          isApprove: '1'
        }
      }
    },
    // 重新修改
    onRemodify (row) {
      if (this.isDraft) {
        this.specialRuleParams = {
          visible: true,
          id: row.relationId,
          params: {
            isApprove: '1',
            orderId: row.orderId,
            draftId: row.draftId
          }
        }
      } else {
        this.specialRuleParams = {
          visible: true,
          id: row.id,
          params: {
            isApprove: '1'
          }
        }
      }
    },
    // 提交审批,获取详情->update->submit
    async onSubmit (row) {
      await approveApis.checkApprove({processType: PROCESS_TYPE.specPrice})

      // 草稿取relationId
      const getInfoID = this.isDraft ? row.relationId : row.id
      let res = {}
      if (!this.isDraft) {
        const getInfoData = await productProgramApis.getInfo({id: getInfoID})
        res = await productProgramApis.update({...getInfoData, isApprove: '1'})
      }
      await approveApis.submit({
        processType: PROCESS_TYPE.specPrice,
        linkName: row.name,
        businessId: getInfoID,
        draftId: this.isDraft ? row.draftId : res.draftId
      })
      this.$message.success('操作成功')
      this.onSearch()
    },
    // 撤销审批
    async onBackApproval (row) {
      await approveApis.cancel({
        businessId: row.relationId,
        draftId: row.draftId,
        processType: PROCESS_TYPE.specPrice
      })
      this.$message.success('操作成功')
      this.onSearch()
    },
    onDetail (row) {
      this.isShowDetail = true
      this.detailID = this.isDraft ? row.relationId : row.id
      this.draftID = this.isDraft ? row.draftId : ''
    }
  }
}
</script>

<style scoped lang="scss"></style>
